package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Workshop;
import com.synergy.stockflow.product.entity.Workstation;
import com.synergy.stockflow.product.service.WorkshopService;
import com.synergy.stockflow.product.service.WorkstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/workstation")
public class WorkstationController {
    @Autowired
    private WorkstationService workstationService;
    @Autowired
    WorkshopService workshopService;
    @GetMapping("/index")
    public Result<Page<Workstation>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                          @RequestParam(name = "size", defaultValue = "5") int size,
                                          @RequestParam(name = "workstation_id", defaultValue = "") String workstationId,
                                          @RequestParam(name = "workstation_name", defaultValue = "") String workstationName,
                                          @RequestParam(name = "workstation_location", defaultValue = "") String workstationLocation,
                                          @RequestParam(name = "is_enabled", defaultValue = "") Integer isEnabled,
                                          @RequestParam(name = "person", defaultValue = "") String person
    ) {
        QueryWrapper<Workstation> query = new QueryWrapper<>();
        if (workstationId != null && workstationId.length() > 0) {
            query.lambda().like(Workstation::getWorkstationId, workstationId);
        }
        if (workstationName != null && workstationName.length() > 0) {
            query.lambda().like(Workstation::getWorkstationName, workstationName);
        }
        if (workstationLocation != null && !workstationLocation.equals("")) {
            query.lambda().like(Workstation::getWorkstationLocation, workstationLocation);
        }
        if (isEnabled != null) {
            query.lambda().like(Workstation::getIsEnabled, isEnabled);
        }

        Page<Workstation> page = new Page<>(current, size);
        page = workstationService.page(page, query);
//        List<Material> list = new ArrayList<>();
//        for (Material record : page.getRecords()) {
//            record.setBoms(bomService.selectspecificationsAndModels(record.getMaterialId()));
//        }
//        page.setRecords(list);
        return Result.success(page);
    }


    @GetMapping("/Name")
    public Result<List<Workshop>>getName(){
        List<Workshop> list =workshopService.list();
        return Result.success(list);
    }
    @GetMapping("/WorkshopName")
    public Result<Page<Workstation>>shopName(@RequestParam(name = "current", defaultValue = "1") int current,
                                             @RequestParam(name = "size", defaultValue = "5") int size,
                                             @RequestParam(name = "workstationId", required = false) String workstationId,
                                             @RequestParam(name = "workstationName", required = false) String workstationName,
                                             @RequestParam(name = "workshopId", required = false) String workshopId,
                                             @RequestParam(name = "processId", required = false) String processId){
        System.out.println("a"+workstationId+workstationName+workshopId+processId);
        Page<Workstation> page = new Page<>(current, size);
        page.setRecords(workstationService.selectWorkshopName(workstationId, workstationName, workshopId, processId));
        return Result.success(page);
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody Workstation workstation) {
        QueryWrapper<Workstation> query = new QueryWrapper<>();
        if (workstation.getWorkstationId() != null && workstation.getWorkstationId().length() > 0) {
            query.lambda().eq(Workstation::getWorkstationId, workstation.getWorkstationId());
        }
        query.lambda().orderBy(true,false, Workstation::getCreateTime);
        Page<Workstation> page = new Page<>(1, 10000);
        Page<Workstation> page1 = workstationService.page(page, query);
        System.out.println(page1);
        if (page1 == null || page1.getRecords().size() == 0) {
            workstation.setCreateTime(new Date());
            workstationService.save(workstation);
            return Result.success(workstation);
        }
        return Result.error(ResultCode.BUSINESS_EXCEPTION.value(), "工作站编码已存在");

    }

    @PutMapping("/update")
    public Result update(@RequestBody Workstation Workstation) {

        workstationService.updateById(Workstation);

        return Result.success(Workstation);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> workstationIds) {
        workstationService.removeBatchByIds(workstationIds);
        return Result.success(workstationIds);
    }
}
